package com.ssyc.gsk_teacher.activity;

import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.utils.SnackbarUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher.R;


/**
 * 教师端--修改小组信息通用界面
 * Created by Administrator on 2018/2/27 0027.
 */

public class TeacherUpdataTeamInfoActivity extends BaseActivity implements View.OnClickListener {


    //返回
    private ImageView ivBack;

    //标题
    private TextView tvTitle;

    //输出框
    private EditText etInput;

    //显示输入框的文字
    private TextView tvNum;

    //保存按钮
    private Button btSave;

    //传递过来的类型区别码
    private int type = -1;

    //通过Intent传值的ResultCode
    public static final int TEAMNAME = 0x210;
    public static final int TEAMSLOGAN = 0x220;
    public static final int TEAMTARGET = 0x230;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return "";
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_updata_team_info;
    }

    @Override
    protected void init() {
        showContent();

        //初始化视图
        initView();

        //设置监听
        initListener();

        //初始状态
        initState();
    }

    /**
     * 初始化视图的方法
     */
    private void initView() {
        etInput = findViewById(R.id.et_edit);
        tvBaseRight.setOnClickListener(this);
        tvTitle = findViewById(R.id.tv_title);
        btSave = findViewById(R.id.bt_commit);
        btSave.setOnClickListener(this);
        tvNum = findViewById(R.id.tv_num);
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
    }


    /**
     * 初始状态
     */
    private void initState() {
        type = getIntent().getIntExtra("type", -1);
        if (type == TeacherEditTeamActivity.TEAMNAME) {
            //编辑小组名称
            tvTitle.setText("编辑小组名称");
            tvNum.setText("0/6");
            etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
        } else if (type == TeacherEditTeamActivity.TEAMSLOGAN) {
            //编辑小组口号
            tvTitle.setText("编辑小组口号");
            tvNum.setText("0/10");
            etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
        } else if (type == TeacherEditTeamActivity.TEAMTARGET) {
            //编辑小组目标
            tvTitle.setText("编辑小组目标");
            tvNum.setText("0/10");
            etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
        }
    }


    /**
     * 设置输入监听
     */
    private void initListener() {
        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = etInput.getText().toString().trim();

                if (type == TeacherEditTeamActivity.TEAMNAME){

                    if (!TextUtils.isEmpty(content)) {
                        if (content.length() > 6) {
                            tvNum.setText("6/6");
                            UiUtils.Toast("最多只能输入6字", false);
                        } else {
                            tvNum.setText(content.length() + "/6");
                        }
                    } else {
                        tvNum.setText("0/6");
                    }

                }else{
                if (!TextUtils.isEmpty(content)) {

                    if (content.length() > 10) {
                        tvNum.setText("10/10");
                        UiUtils.Toast("最多只能输入10字", false);
                    } else {
                        tvNum.setText(content.length() + "/10");
                    }
                } else {
                    tvNum.setText("0/10");
                }
                }
            }
        });
    }


    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.bt_commit) {
            //点击保存后的操作
            //保存
            String content = etInput.getText().toString().trim();
            Intent intent = new Intent();
            if (type == TeacherEditTeamActivity.TEAMNAME) {
                //编辑小组名称
                setResultIntent(content, "请输入小组名称", intent, TEAMNAME);
            } else if (type == TeacherEditTeamActivity.TEAMSLOGAN) {
                //编辑小组口号
                setResultIntent(content, "请输小组口号", intent, TEAMSLOGAN);
            } else if (type == TeacherEditTeamActivity.TEAMTARGET) {
                //编辑小组目标
                setResultIntent(content, "请输入小组目标", intent, TEAMTARGET);
            }
        } else if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }


    /**
     * 统一处理回传信息
     *
     * @param content
     * @param intent
     * @param resultCode
     */
    private void setResultIntent(String content, String message, Intent intent, int resultCode) {
        if (TextUtils.isEmpty(content)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, message, Snackbar.LENGTH_SHORT).show();
            return;
        } else {
            intent.putExtra("data", content);
            setResult(resultCode, intent);
            finish();
        }
    }
}
